package Controller;

import Pojo.Client;
import Pojo.Order;
import Pojo.OrderDetails;
import Service.impl.ClientServiceImpl;
import Service.impl.OrderDetailsServiceImpl;
import Service.impl.OrderServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class OrderAddOneServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        HashMap<String,String> map=new HashMap<>();
        String startDateGet = request.getParameter("startDate");//开始时间
        String endDateGet = request.getParameter("endDate");//结束时间
        String priceSumGet = request.getParameter("priceSum");//总价格
        String roomIdGet = request.getParameter("roomId");//房间id
        String userIdGet = request.getParameter("userId");//用户id
        //
        String cphone = request.getParameter("cphone");
        String cidcard = request.getParameter("cidcard");
        String cname = request.getParameter("cname");
        //根据手机号和身份证去查询是否有该客户，没有则添加
        ClientServiceImpl clientService=new ClientServiceImpl();
        Client client=clientService.ClientQueryByPandC(cphone,cidcard);
        Client client1 =new Client();
        Order order=new Order();
        if(client == null){
            client1.setClient_name(cname);
            client1.setClient_phone(cphone);
            client1.setClient_idcar(cidcard);
            Integer integer=clientService.ClientAddOne(client1);
            if(integer>0){
                order.setClient_id(client1.getClient_id());
            }
        }
        //
        //先去新增order表
        OrderServiceImpl orderService=new OrderServiceImpl();
        order.setUid(Integer.parseInt(userIdGet));
        //mapper开启了数据回填，可以直接拿到id
        Integer integer=orderService.AddOne(order);
        //再去新增order详情表
        Integer odInt=null;
        try {
            OrderDetailsServiceImpl orderDetailsService=new OrderDetailsServiceImpl();
            OrderDetails orderDetails=new OrderDetails();
            orderDetails.setOrder_id(order.getOrder_id());
            //date转型用
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            orderDetails.setStartdate(simpleDateFormat.parse(startDateGet));
            orderDetails.setEnddate(simpleDateFormat.parse(endDateGet));
            orderDetails.setPrice_sum(Integer.parseInt(priceSumGet));
            orderDetails.setRoom_id(Integer.parseInt(roomIdGet));
            odInt=orderDetailsService.AddOne(orderDetails);
            //去调用支付宝二维码
            Map<Object, String> qrmap=orderService.ZfbQrcread(order,orderDetails);
            map.put("QrStr",qrmap.get("QrResult"));
            map.put("orderNumber",qrmap.get("orderNumber"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ////
        String s=odInt>0?"success":"fail";
        map.put("orderResult",s);
        response.getWriter().print(JSON.toJSONString(map));
    }
}
